<?php

//web_content()显示第三个子菜单内容管理的页面内容


//添加action，访问的路径是/wp-admin/admin_ajax.php 方法是参数json其中有action属性，值为beauty_getPositionByTemplate
add_action ( "wp_ajax_beauty_getPositionByTemplate", ajax_getPositionByTemplate );

function ajax_getPositionByTemplate() {
	global $wpdb;
	
	//级联选择框
	$template_id = $_POST ["template_id"];
	//只有当$template_id不为空的时候才去查询相应的内容
	if (! empty ( $template_id )) {
		
		//对数据表bw_template_site的sql查询语句
		$sql_site = "select id,name from bw_template_site where template_id=$template_id";
		$result_site = $wpdb->get_results ( $sql_site );
		
		$sitedata = array ();
		
		foreach ( $result_site as $sites ) {
			$sitedata [] = $sites;
		}
		$str = json_encode ( $result_site );
		echo $str;
	}
}

function web_content() {
	
	global $wpdb;
	
	$site_id = $_POST ["site_id"];
	$template_id = $_POST ["template_id"];
	$deltoken = $_GET ["deltoken"];
	
	// $deltoken等于1为删除位置信息，$deltoken等于2为修改信息$deltoken等于三为添加
	

	//删除内容处理		
	if ($deltoken == "1") {
		$cont_id_delete = $_GET ["cont_id_delete"];
		$template_id = $_GET ["template_id"];
		
		$sqlquery = "delete from  bw_article_content where id=$cont_id_delete";
		$wpdb->query ( $sqlquery );
		$wpdb->flush ();
	} else if ($deltoken == "2") {
		$site_id = $_GET ["site_id"];
		
		$template_id = $_GET ["template_id"];
		$cont_id_update = $_GET ["cont_id_update"];
		$sqlquery = "select * from bw_article_content where id ='$cont_id_update'";
		
		$modcont = $wpdb->get_results ( $sqlquery );
	} else if ($deltoken == "3") {
		$site_id = $_GET ["site_id"];
		$template_id = $_GET ["template_id"];
	}
	if (! empty ( $_POST ["deltoken"] )) {
		$del = $_POST ["deltoken"];
		
		if ($del == "5") {
			
			$cont_title = $_POST ["cont_title"];
			$cont_intro = $_POST ["cont_intro"];
			$cont_content = $_POST ["cont_content"];
			$cont_imguri = $_POST ["cont_imguri"];
			$cont_priority = $_POST ["cont_priority"];
			$content_uri = $_POST ["content_uri"];
			
			$cont_title = preg_replace("/\\\/",'',$cont_title);
			$cont_intro = preg_replace("/\\\/",'',$cont_intro);
			$cont_content = preg_replace("/\\\/",'',$cont_content);
			$cont_imguri = preg_replace("/\\\/",'',$cont_imguri);
			$content_uri = preg_replace("/\\\/",'',$content_uri);
			
			$wpdb->insert ( 'bw_article_content', array ('title' => $cont_title, 'content' => $cont_content, 'article_intro' => $cont_intro, 'img_url' => $cont_imguri, 'site_id' => $site_id, 'priority' => $cont_priority,'content_uri' => $content_uri ) );
		} else if ($del == "4") {
			$wpdb->show_errors();
			$cont_title = $_POST ["cont_title"];
			$content_id = $_POST ["content_id"];
			$cont_intro = $_POST ["cont_intro"];
			$cont_content = $_POST ["cont_content"];
			$cont_imguri = $_POST ["cont_imguri"];
			$cont_priority = $_POST ["cont_priority"];
			$content_uri = $_POST ["content_uri"];
			
			$cont_title = preg_replace("/\\\/",'',$cont_title);
			$cont_intro = preg_replace("/\\\/",'',$cont_intro);
			$cont_content = preg_replace("/\\\/",'',$cont_content);
			$cont_imguri = preg_replace("/\\\/",'',$cont_imguri);
			$content_uri = preg_replace("/\\\/",'',$content_uri);
			
			
	
			$wpdb->update ( 'bw_article_content', array ('title' => $cont_title, 'content' => $cont_content, 'article_intro' => $cont_intro, 'img_url' => $cont_imguri, 'site_id' => $site_id, 'priority' => $cont_priority,'content_uri' => $content_uri ), array ('id' => $content_id ) );
		}
	} else if (! empty ( $deltoken ) && empty ( $site_id )) {
		$site_id = $_GET ["site_id"];
	}
	
	//对数据表bw_template的sql查询语句
	$sql_template = "select id,name from bw_template";
	
	$sql_site = "select id,name from bw_template_site where template_id=$template_id";
	$result_site = $wpdb->get_results ( $sql_site );
	
	//对数据表bw_article_content的sql查询语句
	$sql_cont = "select * from bw_article_content where site_id='$site_id'";
	
	$result_template = $wpdb->get_results ( $sql_template );
	
	$result_cont = $wpdb->get_results ( $sql_cont );
	
	echo "<h2>内容管理</h2>";
	echo '  <script type="text/javascript" language="javascript">     
    var $j = jQuery.noConflict();
    function search(template_id)   
    {    
    	var path ="' . get_bloginfo ( "url" ) . '/wp-admin/admin-ajax.php";
    	var data = {
					action:"beauty_getPositionByTemplate",
					template_id: template_id
		};
	    $j.post(path, data, function(str){
	      		 var siteId=document.getElementById("site_id"); 
	      		 str=str.substring(0,str.length-1); 
 	      		   var objs =  eval(\'(\'+str+\')\');
 	      		   
 	      		   for(var i = 0;i< objs.length;i++){
 	      		       
	             		siteId.options.add(new Option(objs[i].name,objs[i].id));
		            }   
		});
             
    }   
 
    
 function changeCityOptions()   
 {   
     var templateId=document.getElementById("template_id");   
     var siteId=document.getElementById("site_id");   
     siteId.length=0;
     if(templateId.value==-1)   
     {       
         siteId.options.add(new Option("还未选择模板名称","-1"));   
     }   
     else  
     {   
         search(templateId.value);   
     }    
    return;      
 }   
  </script>   
			';
	echo '<form id="static_web" method="post" action="?page=sub-page3" ><select name="template_id" id="template_id"  onchange="changeCityOptions()" ><option value="-1" >请点击选择模板</option>';
	//取得模板name
	foreach ( $result_template as $row_tem ) {
		if ($row_tem->id == $template_id) {
			echo '<option value="' . $row_tem->id . '" selected="selected">' . $row_tem->name . '</option>';
		} else {
			echo '<option value="' . $row_tem->id . '">' . $row_tem->name . '</option>';
		}
	}
	echo '</select><select name="site_id" id="site_id"><option value="-1" >请点击选择位置</option>';
	// 取得位置name
	foreach ( $result_site as $row_site ) {
		if ($site_id == $row_site->id) {
			echo '<option value="' . $row_site->id . '" selected="selected">' . $row_site->name . '</option>';
		} else {
			echo '<option value="' . $row_site->id . '">' . $row_site->name . '</option>';
		}
	}
	echo '</select>';
	echo '<input type="submit" value="确定" class="button-secondary action"/>
	    	<input type="button" value="添加新内容" onclick="javascript:window.location.href=\'?page=sub-page3&deltoken=3&template_id=' . $template_id . '&site_id=' . $site_id . '\';"  class="button-secondary action" />';
	echo '<table  cellspacing="0" class="wp-list-table widefat fixed posts"> 
		<thead>
			<th width="10%">文章标题</th>
			<th width="20%">文章简介</th>
			<th width="25%">文章内容</th>
			<th width="10%">图片地址</th>
			<th width="10%">文章地址</th>
			<th width="10%">显示优先级</th>
			<th width="15%">操作</th>
		</thead>';
	//获得内容表的详细信息、名字、简介、图片地址、先是优先级
	foreach ( $result_cont as $row_cont ) {
		echo '<tr>
			<td>' . $row_cont->title . '</td>
			<td>' . $row_cont->article_intro . '</td>
			<td>' . $row_cont->content . '</td>
			<td>' . $row_cont->img_url. '</td>
			<td>' . $row_cont->content_uri. '</td>
			<td>' . $row_cont->priority . '</td>
			<td>
			  <input type="button" value="修改" onclick="javascript:window.location.href=\'?page=sub-page3&deltoken=2&cont_id_update=' . $row_cont->id . '&site_id=' . $site_id . '&template_id=' . $template_id . '\';"  class="button-secondary action" />
			   <input type="hidden" name="cont_id_update"  value="' . $row_cont->id . '" />
	          <input type="button" value="删除" onclick="javascript:window.location.href=\'?page=sub-page3&deltoken=1&&cont_id_delete=' . $row_cont->id . '&site_id=' . $site_id . '&template_id=' . $template_id . '\';"  class="button-secondary action" />
	          <input type="hidden" name="cont_id_update"  value="' . $row_cont->id . '" />
			</td>
			</tr>';
	}
	echo '</table>';
	//执行修改内容
	if ($deltoken == "2") {
		foreach ( $modcont as $updaterow ) {
			if ($cont_id_update == $updaterow->id) {
				echo '<div class="wrap">
				  <div class="icon32" id="icon-users">
					<br>
				  </div><h2 id="add-new-user">修改文章内容</h2>
					<div id="ajax-response"></div>
					<p>在相应的选框填入相应的修改值。</p>
					<table class="form-table">
						<tbody>
						<tr class="form-field form-required">
							<th scope="row">
								<label for="cont_title">文章标题<span class="description">(必填)</span></label>
							</th>
							<td><input type="text" aria-required="true" value="' . $updaterow->title . '" id="cont_title" name="cont_title" /></td>
						</tr>
						<tr class="form-field form-required">
							<th scope="row">
								<label for="cont_intro">文章简介 <span class="description">(必填)</span></label>
							</th>
							<td><input type="text" value="' . $updaterow->article_intro . '" id="cont_intro" name="cont_intro" /></td>
						</tr>
						<tr class="form-field form-required">
							<th scope="row">
								<label for="cont_content">文章内容<span class="description">(必填)</span></label>
							</th>
							<td><textarea cols="30" rows="5" id="cont_content" name="cont_content" >'.$updaterow->content.' </textarea></td>
						</tr>
						<tr class="form-field form-required">
							<th scope="row">
								<label for="cont_imguri">图片地址<span class="description">(必填)</span></label>
							</th>
							<td><input type="text" aria-required="true" value="' . $updaterow->img_url . '" id="cont_imguri" name="cont_imguri" /></td>
						</tr>
						<tr class="form-field form-required">
							<th scope="row">
								<label for="content_uri">文章地址<span class="description">(必填)</span></label>
							</th>
							<td><input type="text" aria-required="true" value="' . $updaterow->content_uri . '"id="content_uri" name="content_uri" /></td>
						</tr>
						<tr class="form-field">
							<th scope="row">
								<label for="cont_priority">显示优先级<span class="description">(必填)</span></label>
							</th>
							<td>
							<select id="cont_priority" name="cont_priority">
								<option>请选择文章显示优先级</option>
								<option value="1" >1</option>
								<option value="2" >2</option>
								<option value="3" >3</option>
								<option value="4" >4</option>
								<option value="5" >5</option>
								<option value="6" >6</option>
								<option value="7" >7</option>
								<option value="8" >8</option>
								<option value="9" >9</option>
								<option value="10" >10</option>
								<option value="11" >11</option>
								<option value="12" >12</option>
								<option value="13" >13</option>
								<option value="14" >14</option>
								<option value="15" >15</option>
								<option value="16" >16</option>
								<option value="17" >17</option>
								<option value="18" >18</option>
								<option value="19" >19</option>
								<option value="20" >20</option>
								<option value="21" >21</option>
								<option value="22" >22</option>
								<option value="23" >23</option>
								<option value="24" >24</option>
								<option value="25" >25</option>
								<option value="26" >26</option>
								<option value="27" >27</option>
								<option value="28" >28</option>
								<option value="29" >29</option>
								<option value="30" >30</option>
							</select>
						</td>
					</tr>
				</tbody>
				</table></div>';
				echo '<input type="hidden" name="site_id" value="' . $site_id . '"/>';
				echo '<input type="hidden" name="template_id" value="' . $template_id . '"/>';
				echo '<input type="hidden" name="content_id" value="' . $updaterow->id . '"/>';
				echo '<input type="hidden" name="deltoken" value="4" />';
				echo '<p class="submit"><input type="submit" value="保存修改" class="button-primary" id="createusersub" name="addsite"></p>';
				echo '</form> </div>';
			}
		}
	
		//执行添加内容
	} else if ($deltoken == "3") {
		echo '<div class="wrap">
				  <div class="icon32" id="icon-users">
					<br>
				  </div><h2 id="add-new-user">添加文章内容</h2>
					<div id="ajax-response"></div>
					<p>在相应的选框填入相应的修改值。</p>
					<table class="form-table">
						<tbody>
						<tr class="form-field form-required">
							<th scope="row">
								<label for="cont_title">文章标题<span class="description">(必填)</span></label>
							</th>
							<td><input type="text" aria-required="true" value="' . $updaterow->title . '" id="cont_title" name="cont_title" /></td>
						</tr>
						<tr class="form-field form-required">
							<th scope="row">
								<label for="cont_intro">文章简介 <span class="description">(必填)</span></label>
							</th>
							<td><input type="text" value="' . $updaterow->article_intro . '" id="cont_intro" name="cont_intro" /></td>
						</tr>
						<tr class="form-field form-required">
							<th scope="row">
								<label for="cont_content">文章内容<span class="description">(必填)</span></label>
							</th>
							<td><textarea cols="30"  rows="5" id="cont_content" name="cont_content" >'. $updaterow->content .'</textarea></td>
						</tr>
						<tr class="form-field form-required">
							<th scope="row">
								<label for="cont_imguri">图片地址<span class="description">(必填)</span></label>
							</th>
							<td><input type="text" aria-required="true" value="' . $updaterow->img_url . '" id="cont_imguri" name="cont_imguri" /></td>
						</tr>
						<tr class="form-field form-required">
							<th scope="row">
								<label for="content_uri">文章地址<span class="description">(必填)</span></label>
							</th>
							<td><input type="text" aria-required="true" value="' . $updaterow->content_uri. '" id="content_uri" name="content_uri" /></td>
						</tr>
						<tr class="form-field">
							<th scope="row">
								<label for="cont_priority">显示优先级<span class="description">(必填)</span></label>
							</th>
							<td>
							<select id="cont_priority" name="cont_priority">
								<option>请选择文章显示优先级</option>
								<option value="1" >1</option>
								<option value="2" >2</option>
								<option value="3" >3</option>
								<option value="4" >4</option>
								<option value="5" >5</option>
								<option value="6" >6</option>
								<option value="7" >7</option>
								<option value="8" >8</option>
								<option value="9" >9</option>
								<option value="10" >10</option>
								<option value="11" >11</option>
								<option value="12" >12</option>
								<option value="13" >13</option>
								<option value="14" >14</option>
								<option value="15" >15</option>
								<option value="16" >16</option>
								<option value="17" >17</option>
								<option value="18" >18</option>
								<option value="19" >19</option>
								<option value="20" >20</option>
								<option value="21" >21</option>
								<option value="22" >22</option>
								<option value="23" >23</option>
								<option value="24" >24</option>
								<option value="25" >25</option>
								<option value="26" >26</option>
								<option value="27" >27</option>
								<option value="28" >28</option>
								<option value="29" >29</option>
								<option value="30" >30</option>
							</select>
						</td>
					</tr>
				</tbody>
				</table></div>';
		echo '<input type="hidden" name="site_id" value="' . $site_id . '"/><input type="hidden" name="deltoken" value="5" />';
		echo '<p class="submit"><input type="submit" value="添加新内容" class="button-primary" id="createusersub" name="addsite"></p>';
		echo '</form> </div>';
	}
} 
  